package org.example.spring.simple.demon.controller;

import org.example.spring.simple.demon.service.IModifyService;
import org.example.spring.simple.demon.service.IQueryService;
import org.example.spring.simple.framework.annotation.ZAutowired;
import org.example.spring.simple.framework.annotation.ZController;
import org.example.spring.simple.framework.annotation.ZRequestMapping;
import org.example.spring.simple.framework.annotation.ZRequestParam;
import org.example.spring.simple.framework.webmvc.servlet.ZModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 公布接口url
 * @author Tom
 *
 */
@ZController
@ZRequestMapping("/web")
public class DemoController {

    @ZAutowired
    IQueryService queryService;
    @ZAutowired
    IModifyService modifyService;

    @ZRequestMapping("/first.html")
    public ZModelAndView query1(@ZRequestParam String person) {
        String result = queryService.query(person);
        Map<String, Object> model = new HashMap<>();
        model.put("person", person);
        model.put("data", result);
        model.put("token", UUID.randomUUID());
        return new ZModelAndView("first.html", model);
    }

    @ZRequestMapping("/query.json")
    public ZModelAndView query(HttpServletRequest request, HttpServletResponse response,
                               @ZRequestParam("name") String name){
        String result = queryService.query(name);
        return out(response,result);
    }

    @ZRequestMapping("/add*.json")
    public ZModelAndView add(HttpServletRequest request,HttpServletResponse response,
                              @ZRequestParam("name") String name,@ZRequestParam("addr") String addr){
        String result = null;
        try {
            result = modifyService.add(name,addr);
            return out(response,result);
        } catch (Exception e) {
            Map<String,Object> model = new HashMap<>();
            model.put("detail",e.getCause().getMessage());
            model.put("stackTrace", Arrays.toString(e.getStackTrace()).replaceAll("\\[|\\]",""));
            return new ZModelAndView("500", model);
        }

    }

    @ZRequestMapping("/remove.json")
    public ZModelAndView remove(HttpServletRequest request,HttpServletResponse response,
                                 @ZRequestParam("id") Integer id){
        String result = modifyService.remove(id);
        return out(response,result);
    }

    @ZRequestMapping("/edit.json")
    public ZModelAndView edit(HttpServletRequest request,HttpServletResponse response,
                               @ZRequestParam("id") Integer id,
                               @ZRequestParam("name") String name){
        String result = modifyService.edit(id,name);
        return out(response,result);
    }



    private ZModelAndView out(HttpServletResponse resp,String str){
        try {
            resp.getWriter().write(str);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}
